package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.api.Code;
import com.mall.common.api.Result;
import com.mall.dto.SmsCouponDto;
import com.mall.dto.SmsProductDto;
import com.mall.entity.SmsCoupon;
import com.mall.entity.SmsCouponHistory;
import com.mall.service.IUserService;
import com.mall.service.SmsCouponHistoryService;
import com.mall.service.SmsCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "SmsCouponCategoryController", description = "优惠券历史记录管理")
@RestController
@RequestMapping("/couponhistories")
public class SmsCouponHistoryController {
    @Resource
    private SmsCouponHistoryService smsCouponHistoryService;

    @Resource
    private SmsCouponService smsCouponService;

    @Resource
    private IUserService iUserService;

    @ApiOperation(value = "新增优惠券历史记录")
    @PostMapping
    public Result save(@RequestBody SmsCouponHistory smsCouponHistory) {
        boolean flag = smsCouponHistoryService.save(smsCouponHistory);
        return new Result(flag? Code.SAVE_OK: Code.SAVE_ERR,flag);
    }

    @ApiOperation(value = "领取优惠券")
    @GetMapping("/receiveCoupon")
    public Result receiveCoupon(@RequestParam Long memberId,
                                @RequestParam Long couponId){
        //生成优惠券code
        long first14 = (long) (Math.random() * 100000000000000L);
        long number = 5200000000000000L + first14;
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String current = formatter.format(date);
        String nickName = iUserService.getById(memberId).getNickname();
        SmsCoupon coupon = smsCouponService.getById(couponId);
        coupon.setReceiveCount(coupon.getReceiveCount()+1);
        coupon.setCount(coupon.getCount()-1);
        boolean flag1 = smsCouponService.save(coupon);
        SmsCouponHistory history = new SmsCouponHistory();
        history.setCouponId(couponId);
        history.setCouponCode(coupon.getCode());
        history.setGetType(1);
        history.setCreateTime(current);
        history.setMemberNickname(nickName);
        history.setMemberId(memberId);
        history.setCouponCode(Long.toString(number));
        boolean flag2 = smsCouponHistoryService.save(history);
        return new Result(flag1&&flag2? Code.SAVE_OK: Code.SAVE_ERR,flag1&&flag2);
    }

    @ApiOperation(value = "根据id删除优惠券历史记录")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = smsCouponHistoryService.deleteById(id);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @ApiOperation(value = "根据会员id查找优惠券")
    @GetMapping("/getByMemberId/{memberId}")
    public Result getByMemberId(@PathVariable Long memberId) {
        List<SmsCouponHistory> list = smsCouponHistoryService.getByMemberId(memberId);
        List<SmsCouponDto> couponList = new ArrayList<>();
        for(int i=0;i<list.size();i++)
        {
            SmsCouponDto smsCouponDto = new SmsCouponDto();
            SmsCoupon coupon = smsCouponService.getById(list.get(i).getCouponId());
            smsCouponDto.setCode(coupon.getCode());
            smsCouponDto.setAmount(coupon.getAmount());
            smsCouponDto.setEndTime(coupon.getEndTime());
            smsCouponDto.setEnableTime(coupon.getEnableTime());
            smsCouponDto.setMinPoint(coupon.getMinPoint());
            smsCouponDto.setCouponId(coupon.getId());
            smsCouponDto.setName(coupon.getName());
            smsCouponDto.setPlatform(coupon.getPlatform());
            smsCouponDto.setStartTime(coupon.getStartTime());
            smsCouponDto.setNote(coupon.getNote());
            smsCouponDto.setUseStatus(smsCouponHistoryService.getByIds(memberId,coupon.getId()).getUseStatus());
            couponList.add(smsCouponDto);
        }
        Integer code = couponList !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = couponList !=null ? "successful":"failed,please try again";
        return new Result(couponList,code,msg);
    }

    @ApiOperation(value = "根据会员id查找可用优惠券")
    @GetMapping("/getAbleCoupon/{memberId}")
    public Result getAbleCoupon(@PathVariable Long memberId) {
        List<SmsCouponHistory> list = smsCouponHistoryService.getByMemberId(memberId);
        List<SmsCouponDto> couponList = new ArrayList<>();
        for(int i=0;i<list.size();i++)
        {
            SmsCouponDto smsCouponDto = new SmsCouponDto();
            SmsCoupon coupon = smsCouponService.getById(list.get(i).getCouponId());
            smsCouponDto.setCode(coupon.getCode());
            smsCouponDto.setAmount(coupon.getAmount());
            smsCouponDto.setEndTime(coupon.getEndTime());
            smsCouponDto.setEnableTime(coupon.getEnableTime());
            smsCouponDto.setMinPoint(coupon.getMinPoint());
            smsCouponDto.setCouponId(coupon.getId());
            smsCouponDto.setName(coupon.getName());
            smsCouponDto.setPlatform(coupon.getPlatform());
            smsCouponDto.setStartTime(coupon.getStartTime());
            smsCouponDto.setNote(coupon.getNote());
            smsCouponDto.setUseStatus(smsCouponHistoryService.getByIds(memberId,coupon.getId()).getUseStatus());
            if(smsCouponDto.getUseStatus()==0){
                couponList.add(smsCouponDto);
            }
        }
        Integer code = couponList !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = couponList !=null ? "successful":"failed,please try again";
        return new Result(couponList,code,msg);
    }

    @ApiOperation(value="根据优惠券id和成员id查询优惠券历史记录")
    @GetMapping("/getByIds")
    public Result getByIds(@RequestParam Long memberId,
                           @RequestParam Long couponId){
        SmsCouponHistory smsCouponHistory = smsCouponHistoryService.getByIds(memberId,couponId);
        Integer code = smsCouponHistory !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = smsCouponHistory !=null ? "successful":"failed,please try again";
        return new Result(smsCouponHistory,code,msg);
    }

    @ApiOperation(value = "根据id查找优惠券历史记录")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        SmsCouponHistory smsCouponHistory = new SmsCouponHistory();
        smsCouponHistory = smsCouponHistoryService.getById(id);
        Integer code = smsCouponHistory !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = smsCouponHistory !=null ? "successful":"failed,please try again";
        return new Result(smsCouponHistory,code,msg);
    }

    @ApiOperation(value = "查找所有优惠券历史记录")
    @GetMapping
    public Result getAll(){
        List<SmsCouponHistory> list = smsCouponHistoryService.getAll();
        Integer code = list!=null ? Code.GET_OK:Code.GET_ERR;
        String msg = list!=null ? "successful":"failed,please try again";
        return new Result(list,code,msg);
    }

    @PostMapping ("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        boolean flag = smsCouponHistoryService.removeByIds(ids);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @GetMapping("/page")
    public IPage<SmsCouponHistory> getPage(@RequestParam Integer pageNum,
                                           @RequestParam Integer pageSize,
                                           @RequestParam String couponCode,
                                           @RequestParam Integer couponId) {
        IPage<SmsCouponHistory> page = new Page<>(pageNum,pageSize);
        QueryWrapper<SmsCouponHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("coupon_code", couponCode);
        queryWrapper.eq("coupon_id", couponId);
        return smsCouponHistoryService.page(page,queryWrapper);
    }
}
